package com.graduation.project.handler;

import com.alibaba.fastjson.JSON;
import com.graduation.project.utils.ResultUtil;
import com.graduation.project.utils.WebUtil;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 卑微小峰
 * @date 2022/10/15
 * 鉴权失败处理器
 */
@Component
public class AccessDeniedFailedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        ResultUtil result = new ResultUtil(HttpStatus.FORBIDDEN.value(), "您的权限不足");

        // 处理异常
        WebUtil.renderString(response, JSON.toJSONString(result));
    }
}
